<template>
  <div class="login-content">
    <video src="https://slwl-admin.itheima.net/static/media/video.3cf57b17.mp4" autoplay loop muted />
    <el-card class="box-card">
      <img src="../../assets/login.png" class="loginLogo">
      <el-form ref="form" label-width="100px" class="loginForm" label-position="top" :model="form" :rules="rules">
        <el-form-item prop="account">
          <el-input v-model="form.account" prefix-icon="el-icon-s-custom" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" show-password prefix-icon="el-icon-unlock" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="form.code" placeholder="请输入验证码" prefix-icon="el-icon-message-solid" class="code" />
          <!-- <el-image v-if="codes.isShow" style="height: 42px" :src="codes.src" /> -->
          <img v-if="codes.isShow" style="height: 42px" :src="codes.src">
          <el-button v-else class="btnCode" @click="btnCode">验证码</el-button>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="btnLogin">登录</el-button>
    </el-card>
  </div>
</template>

<script>
import { getCaptcha } from '@/api/authentication'
import { mapActions } from 'vuex'

export default {
  name: 'LoginPage',

  data() {
    return {
      // form表单
      form: {
        account: process.env.NODE_ENV === 'development' ? 'shenlingadmin' : '',
        password: process.env.NODE_ENV === 'development' ? '123456' : '',
        code: '',
        key: ''
      },
      // 表单校验
      rules: {
        account: [{ required: true, message: '不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      // 验证码
      codes: {
        isShow: false,
        src: ''
      }
    }
  },

  methods: {
    ...mapActions('user', ['getUserInfo']),

    // 点击确定
    async btnLogin() {
      await this.$refs.form.validate()
      await this.getUserInfo(this.form)
      this.codes.isShow = false
      this.$refs.form.resetFields()
      this.$message.success('登录成功')
      this.$router.push('/')
    },

    // 点击验证码
    async btnCode() {
      const key = Math.floor(Math.random() * 9999)
      const result = await getCaptcha(key)
      this.codes.isShow = true
      this.codes.src = result.request.responseURL
      this.form.key = key + ''

      setTimeout(() => {
        this.codes.isShow = false
        this.codes.src = ''
      }, 60000)
    }
  }
}
</script>

<style lang="scss" scoped>
.loginLogo {
  width: 195px;
  height: 120px;
  display: inline-block;
  margin: 10px 90px;
}

.box-card {
  width: 450px;
  padding: 20px;
  z-index: 999;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;
  box-shadow: 3px 5px 5px #333;
}

video {
  width: 100%;
  height: 100%;
  object-fit: fill;
}

.title {
  color: #1890ff;
  font-size: 30px;
  text-align: center;
  margin: 40px 0 30px;
}

::v-deep .el-button {
  width: 100%;
  height: 50px;
}

::v-deep .el-input__inner {
  height: 50px;
  padding: 0 45px;
}

::v-deep .el-input__icon {
  font-size: 20px;
  line-height: 50px;
  margin: 0 5px;
}

::v-deep .code {
  display: inline-block;
  width: 60%;
}

::v-deep img {
  display: inline-block;
  margin: -10px 0;
  height: 40px;
  margin-left: 2%;
}

::v-deep .btnCode {
  display: inline-block;
  width: 38%;
  height: 50px;
  margin-left: 2%;
}
</style>
